package cn.itsource.order.controller;

import cn.itsource.base.util.AjaxResult;
import cn.itsource.base.util.JwtUtils;
import cn.itsource.base.util.LoginData;
import cn.itsource.base.util.Payload;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.domain.dto.OrderPetConfirmDto;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.system.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/orderPetAcquisition")
public class OrderPetAcquisitionController {

    @Autowired
    public IOrderPetAcquisitionService orderPetAcquisitionService;

    /**
     * 确认宠物收购
     * @return
     */
    @PostMapping("/confirm")
    public AjaxResult confirm(@RequestBody OrderPetConfirmDto dto){
        return orderPetAcquisitionService.confirm(dto);
    }

    /**
     * 接口：添加或修改
     * @param orderPetAcquisition  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderPetAcquisition orderPetAcquisition){ 
        if( orderPetAcquisition.getId()!=null){
		orderPetAcquisitionService.update(orderPetAcquisition);
        }else{
                orderPetAcquisitionService.save(orderPetAcquisition);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        orderPetAcquisitionService.deleteById(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        orderPetAcquisitionService.batchDeleteByIds(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public OrderPetAcquisition findOne(@PathVariable("id")Long id) {
        return orderPetAcquisitionService.selectById(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<OrderPetAcquisition> findAll(){
        return orderPetAcquisitionService.selectAll();
    }


    @PostMapping("/pageList")
    public AjaxResult pageList(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Payload<LoginData> loginDataPayload = JwtUtils.checkToken(token);
        LoginData loginData = loginDataPayload.getLoginData();
        Logininfo logininfo = loginData.getLogininfo();
        return orderPetAcquisitionService.pageList(query,logininfo.getId());
    }
}
